var AdDetail = {
	adId : null,
	budget : null,
	load : function () {
		var qs =  new QueryString();
		AdDetail.adId = qs.get("adId");
		this.eventRegister();
		this.setAdData(AdDetail.adId);
	},
	
	setAdData : function (adId) {
		var mask = createMask();
		mask.show();
		Ext.Ajax.request({
			url : "/ad/sys/com/control/AdService.php?action=getAdById&adId=" + adId,
			callback : function (op, su, rsp) {
				mask.hide();
				var ad  =  rsp.responseText.toJSON();
			    var sexText = "";
			    
			    switch (ad.Sex) {
			    	case '0' : sexText = "不分男女"; break;
			    	case '1' : sexText = "男姓"; break;
			    	case '2' : sexText = "女姓"; break;
			    }
				 
				Ext.getDom("Image1").src = ad.Image1;
				Ext.getDom("Image2").src = ad.Image2;
				Ext.getDom("AdName").value = ad.AdName;
				Ext.getDom("adInfo").value = ad.AgeStart + "歲~" + ad.AgeEnd+"歲" + " " + sexText;
				Ext.getDom("area").value  = ad.AreaName;
				Ext.getDom("AdBillingType").value = ad.AdBillingType;
				Ext.getDom("AdStartDate").value = ad.AdStartDate;
				Ext.getDom("AdEndDate").value = ad.AdEndDate;
				Ext.getDom("Budget").value = ad.Budget;
				Ext.getDom("Description").value = ad.Description;
				addOptions("Status",
					[
						{value : 0 , text:  "審理中"},
						{value : 1 , text : "刊登中"},
						{value : 2 , text : "已結束"},
						{value : 3 , text : "暫停"},
						{value : 4 , text : "刪除" },
						{value : 5 , text : "未通過" },
						{value : 6 , text : "需修改" }
					],
					"value" , "text"
				)
				setOptions("Status", ad.Status);
				
			}
		});
	},
	
    close : function () {
   		window.close();
    },
	
    next : function () {
    	if (!confirm("確認更新?")) return;
    	
    	var status = getOptionValue("Status");
    	var desc =  Ext.getDom("Description").value;
    	var adId = AdDetail.adId; 
    	var budget =  Ext.getDom("Budget").value;
    	var isNotifyBudget = AdDetail.budget != budget ;
    	 
    	
    	waitShow("更新中,請稍待....")
    	Ext.Ajax.request({
    		url : "/ad/sys/com/control/AdService.php?action=updateAd",
    		method : "post",
    		params : {
    			status : getOptionValue("Status"),
    			description   : Ext.getDom("Description").value,
    			adId   : AdDetail.adId,
    			adName : Ext.getDom("AdName").value ,
    			isNotifyBudget : isNotifyBudget,
    			budget : budget
    		},
    		callback : function (op, su, rsp) {
    			waitHide();
    			var obj  =  rsp.responseText.toJSON();
    			if (obj.result == 1) {
    				alert("更新成功!");
    			} else {
    				alert("更新失敗!");
    			}
    			window.close();
    		}
    	});
    	
    },
    
	eventRegister : function () {
		Ext.get("btnClose").on('click',this.close);  //確認,新增使用者		
		Ext.get("btnNext").on("click",this.next); //檢查使用者帳號
	}
}

Ext.onReady(function (){
	AdDetail.load();
});